//
//  TouTiaoListViewController.m
//  MoBang
//
//  Created by ferver7 on 2018/7/23.
//  Copyright © 2018年 ferver7. All rights reserved.
//

#import "TouTiaoListViewController.h"
#import "XFWkwebView.h"

@interface TouTiaoListViewController ()
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic,strong) NSMutableArray *noticeArr;
@property (assign, nonatomic) NSInteger page;
@end

@implementation TouTiaoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    mNavitationTitle(@"嗒嗒头条");
    self.page = 1;
    [self getTouTiaoData];
    self.selfTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getTouTiaoData];
    }];
    self.selfTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self getTouTiaoData];
    }];
    self.selfTableView.tableFooterView = [UIView new];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.noticeArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = mGetTableViewCell(@"lineCell");
    UIImageView * logoImageView = mViewByTag(cell.contentView, 100, UIImageView);
    NSDictionary * infodic = self.noticeArr[indexPath.row];
    [logoImageView sd_setImageWithURL:[NSURL URLWithString:infodic[@"imgUrl"]] placeholderImage:mImageByName(@"xiaofang")];
    
//    NSDictionary * infodic = self.noticeArr[indexPath.row];imgUrl
    
    UILabel * titleLabel = mViewByTag(cell.contentView, 101, UILabel);
    titleLabel.font = mTitleFont(14);
    titleLabel.textColor = [UIColor AxcUI_colorWithHexCode:@"#333333"];
    titleLabel.text = infodic[@"title"];
    
    UILabel * timeLabel = mViewByTag(cell.contentView, 102, UILabel);
    
    timeLabel.text = infodic[@"createTime"];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 88;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XFWkwebView * web = [[XFWkwebView alloc]init];
    NSDictionary * infodic = self.noticeArr[indexPath.row];
    web.oid = infodic[@"would"];
    [web loadInfoHTMLSring:infodic[@"would"]];
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark -
#pragma mark - **************** 获取头条列表
-(void)getTouTiaoData
{
    
    if ([self.selfTableView.mj_header isRefreshing]) {
        self.page = 1;
    }
    if ([self.selfTableView.mj_footer isRefreshing]) {
        self.page ++;
    }
    
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
    setDickeyobj(requestInfo, @"10", @"limit");
    NSString *currpageStr = [NSString stringWithFormat:@"%ld",self.page];
    setDickeyobj(requestInfo,currpageStr, @"page");
    setDickeyobj(requestInfo, @"", @"key");
    [PPNetworkHelper POST:app_getHeadlines parameters:requestInfo success:^(id responseObject) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
        if (self.page == 1 && self.noticeArr.count > 0) {
             [self.noticeArr removeAllObjects];
        }
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * infoDic in responseObject[@"data"][@"list"]) {
                [self.noticeArr addObject:infoDic];
            }
            if ([responseObject[@"data"][@"list"] count] < 10) {
                [self.selfTableView.mj_footer endRefreshingWithNoMoreData];
                
            }

        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        [self.selfTableView.mj_header endRefreshing];
        [self.selfTableView.mj_footer endRefreshing];
    } showLoading:nil];
}
-(NSMutableArray *)noticeArr
{
    if (!_noticeArr) {
        _noticeArr = [[NSMutableArray alloc]init];
        
    }
    return _noticeArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
